import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";

export default defineConfig({
  plugins: [vue()],
  build: {
    outDir: "dist",
    emptyOutDir: true,
  },
  server: {
    host: "0.0.0.0", // 允许外部访问
    port: 3000,
    strictPort: true, // 强制使用3000端口，不自动切换
    proxy: {
      // API代理配置
      "/api": {
        target: "http://127.0.0.1:3001",
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, "/api"),
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('代理错误:', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('代理请求:', req.method, req.url);
          });
        }
      },
      // 静态文件代理
      "/uploads": {
        target: "http://127.0.0.1:3001",
        changeOrigin: true,
        secure: false,
      },
      "/output": {
        target: "http://127.0.0.1:3001",
        changeOrigin: true,
        secure: false,
      },
      // 数据库文件代理（如果需要）
      "/data": {
        target: "http://127.0.0.1:3001",
        changeOrigin: true,
        secure: false,
      }
    },
    // 开发服务器配置
    cors: true,
    hmr: {
      overlay: true, // 显示错误覆盖层
    },
  },
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
    },
  },
});
